package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.crypto.interfaces.PBEKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    DtsBrandService brandService;

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsCategoryService categoryService;

    @RequestMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        Object res = goodsService.add(goodsAllinone);
        return res;
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){

        if(goods.getId() == null){
            return ResponseUtil.badArgument();
        }

        goodsService.delete(goods.getId());
        return ResponseUtil.ok();
    }

    @RequestMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){

        return goodsService.update(goodsAllinone);

    }

    @RequestMapping("/catAndBrand")
    public Object catAndBrand(){

        List<CatVo> categoryList = categoryService.findCategoryList();

        List<Map<String, Object>> brandList = brandService.findBrandList();

        Map<String, Object> data = new HashMap<>();
        data.put("categoryList",categoryList);
        data.put("brandList",brandList);

        return ResponseUtil.ok(data);
    }

    @RequestMapping("/detail")
    public Object detail(Integer id){
        Map<String, Object> data = goodsService.findById(id);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(name = "limit", required = false, defaultValue = "20") Integer limit,
                       @RequestParam(name = "sort", required = false, defaultValue = "add_time") String sort,
                       @RequestParam(name = "order", required = false, defaultValue = "desc") String order,
                       String goodsSn,
                       String name){

        //是否是品牌管理员
        Subject subject = SecurityUtils.getSubject();

        List<Integer> brandIds = null;
        if(subject.hasRole("品牌制造商") && !subject.hasRole("超级管理员")){
            //查询所有的品牌id
            brandIds = brandService.findBrandIds();
        }

        IPage<DtsGoods> iPage = goodsService.queryPage(page, limit, sort, order, goodsSn, name, brandIds);

        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return ResponseUtil.ok(data);

    }
}
